package cn.aitrox.ry.service.anno.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.anno.api.AnnoResultApi;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultExportInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultReadInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultSearchInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoResultSubmitInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoSeriesGetInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoUsersInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.AnnoYayInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.in.DeepzoomUrlInDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.AnnoResultDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.AnnoResultExportResultOutDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.DeepzoomUrlOutDto;
import cn.aitrox.ry.service.anno.dto.anno.result.out.SeriesGetResultDto;
import cn.aitrox.ry.service.anno.entity.AnnoResultEntity;
import cn.aitrox.ry.service.anno.enumtype.anno.YayEnum;
import cn.aitrox.ry.service.anno.service.AnnoResultService;
import cn.aitrox.ry.util.ArrayUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AnnoResultController extends BaseController implements AnnoResultApi {

    @Autowired
    private AnnoResultService annoResultService;

    @PostMapping(value = "/anno/result/series/get")
    @Override
    public RespResult<SeriesGetResultDto> seriesGet(@Validated @RequestBody AnnoSeriesGetInDto input) {
        // 锁住，按照任务锁
        synchronized (input.getTaskId().toString().intern()) {
            return annoResultService.seriesGet(getUserId(), input.getTaskId());
        }
    }

    @PostMapping(value = "/anno/result/read")
    @Override
    public RespResult<AnnoResultDto> read(@Validated @RequestBody AnnoResultReadInDto input) {
        return annoResultService.read(input.getArId());
    }

    @PostMapping(value = "/anno/result/yay")
    @Override
    public RespResult yay(@Validated @RequestBody AnnoYayInDto input) {
        // 校验参数
        if (null == YayEnum.getEnumByValue(input.getYay())) {
            return RespResult.error(RespCodeEnum.PARAM_ERROR);
        }
        RespResult<AnnoResultEntity> checkResult = annoResultService.checkAr(getUserId(), input.getArId());
        if (!checkResult._isOk()) {
            return RespResult.secByError(checkResult.getCode(), checkResult.getMsg());
        }

        annoResultService.yay(input.getArId(), input.getYay());

        return RespResult.ok();
    }

    @PostMapping(value = "/anno/result/submit")
    @Override
    public RespResult submit(@Validated @RequestBody AnnoResultSubmitInDto input) {
        List<AnnoResultEntity> arList = annoResultService.getAnnoResultEntitys(input.getArIdList());
        // 锁住，按照任务锁
        synchronized (arList.get(0).getTaskId().toString().intern()) {
            return annoResultService.submit(super.getUserId(), arList, input.getStatus(), input.getDiscardCode(), input.getDiscardRemark());
        }
    }

    @PostMapping(value = "/anno/result/users")
    @Override
    public RespResult<List<Integer>> annoUsers(@Validated @RequestBody AnnoUsersInDto input) {
        List<Integer> userIds = annoResultService.getUserIds(input.getTaskId());
        return RespResult.ok(userIds);
    }

    @PostMapping(value = "/anno/result/search")
    @Override
    public CommonPageInfo<AnnoResultEntity> search(@Validated @RequestBody AnnoResultSearchInDto input) {
        PageInfo<AnnoResultEntity> pageInfo = annoResultService.search(input);
        if (ArrayUtil.isEmpty(pageInfo.getList())) {
            return new CommonPageInfo(0, 0, null);
        }
        return new CommonPageInfo<>(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @PostMapping(value = "/anno/result/export")
    @Override
    public AnnoResultExportResultOutDto export(@Validated @RequestBody AnnoResultExportInDto input) {
        return annoResultService.export(input.getTaskIds());
    }

    @Override
    public RespResult<DeepzoomUrlOutDto> deepzoomUrl(DeepzoomUrlInDto input) {
        return annoResultService.deepzoomUrl(input.getTaskId(), input.getSeriesUid());
    }
}
